﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.Specialized;
using SharePoint2007Migration.Domain.Collections;
using SharePoint2007Migration.Exceptions;

namespace SharePoint2007Migration.Domain
{
    public abstract class BaseReport
    {
        IssueCollection _issues;
        StringCollection _siteCollectionUrls;
        string _contentDbConnectionString;

        public BaseReport(IssueCollection Issues)
        {
            _issues = Issues;
        }

        public IssueCollection Issues { get { return _issues; } }

        public abstract string CreateReport();

        public StringCollection SiteCollectionUrls 
        {
            get
            {
                if (_siteCollectionUrls == null)
                    _siteCollectionUrls = GetSiteCollectionUrlsByContentDbName(_issues.ContentDbName);
                return _siteCollectionUrls;
            }
           
        }

        public string ContentDbConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_contentDbConnectionString))
                {
                    _contentDbConnectionString = GetConnectionStringByContentDbName(_issues.ContentDbName);
                    if (String.IsNullOrEmpty(_contentDbConnectionString))
                        throw new NotFoundContentDatabaseException(_issues.ContentDbName);
                }
                return _contentDbConnectionString;
            }
        }

        protected StringCollection GetSiteCollectionUrlsByContentDbName(string contentDbName)
        {
            StringCollection siteUrls = new StringCollection();
            SPWebApplicationCollection sPWebAppCollection = SPWebService.ContentService.WebApplications;
            foreach (var webApp in sPWebAppCollection)
	        {
                foreach (var contetnDb in webApp.ContentDatabases)
	            {
                    if (contetnDb.Name.ToLower().Equals(contentDbName.ToLower()))
	                {
                        foreach (SPSite ssite in contetnDb.Sites)
                        {
                            siteUrls.Add(ssite.Url);
                        }
                    }

	            }
	        }
            return siteUrls;
        }

        protected string GetConnectionStringByContentDbName(string contentDbName)
        {
            LogHelper.TraceDebug(String.Format("Trying to resolve the connection string for {0}", contentDbName));
            string connectionString = string.Empty;
            SPWebApplicationCollection sPWebAppCollection = SPWebService.ContentService.WebApplications;
            if (sPWebAppCollection != null)
                LogHelper.TraceDebug(String.Format("Will search for the current contet db in {0} webapplications", sPWebAppCollection.Count));
            else LogHelper.TraceError(new Exception("Unable to get the Web Application list in the current farm"));

            foreach (var webApp in sPWebAppCollection)
            {
                LogHelper.TraceDebug(String.Format("Searching in {0}", webApp.Name));
                foreach (var contentDb in webApp.ContentDatabases)
                {
                    LogHelper.TraceDebug(String.Format("Found {0} database in current farm", contentDb.Name));
                    if (contentDb.Name.Equals(contentDbName,StringComparison.InvariantCultureIgnoreCase))
                    {
                        connectionString = contentDb.DatabaseConnectionString;
                        LogHelper.TraceDebug("ok! connection string found!!!!!");
                        break;
                    }
                }
            }
            if (connectionString == string.Empty) LogHelper.TraceDebug("Unable to find a valid connection string for your content database..");
            return connectionString;
        }


        protected string CreateReportBanner(string title)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("###############################");
            sb.AppendLine(String.Format("###   {0}  ###", title));
            sb.AppendLine("###############################");
            return sb.ToString();
 
        }

        protected string CreateIssueCountInformations(int IssuesFoundCount, int IssueResolvedCount, int CheckedAginstCount, string IssueType)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(" [Informations]");
            sb.AppendLine(String.Format("-> {0} {1} issues have been parsed from imput file.", IssuesFoundCount, IssueType));
            sb.AppendLine(String.Format("-> {0} {1} have been found into your content db ", CheckedAginstCount, IssueType));
            sb.AppendLine(String.Format("-> {0} {1}s have been analized", IssueResolvedCount, IssueType));
            sb.AppendLine();
            return sb.ToString();
        }

       
    }
}
